package com.itheima.listener;



import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

//这是仿springweb的封装.到时候直接用它的就行
public class ContextloaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        //获取web.xml中配置的全局初始化参数
        String contextConfiguration = servletContext.getInitParameter("contextConfigLocation");
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); //直接写配置文件名,不好
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfiguration);
        //将应用上下文对象存储到servletContext作用域中
        servletContext.setAttribute("app",app);
        System.out.println("监听器初始化了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContextListener.super.contextDestroyed(sce);
    }
}
